package com.lanyou.esb.cook.proxy.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.common.DateUtils;
import com.lanyou.esb.cook.proxy.dao.impl.BaseDao;
import com.lanyou.esb.cook.proxy.entity.IfAlarm;
import com.lanyou.esb.cook.proxy.entity.IfAlarmWorkOrder;
import com.lanyou.esb.cook.proxy.exception.PersistentDataException;
import com.lanyou.esb.cook.proxy.meta.EnableType;
import com.lanyou.esb.cook.proxy.meta.WoStatus;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.WorkOrderNoJedisProxy;
import com.lanyou.esb.cook.proxy.service.IfAlarmWorkOrderService;

/**
 * 接口告警工单
 * 
 * @author Davey.wu
 */
public class IfAlarmWorkOrderServiceImpl implements IfAlarmWorkOrderService {
	/** 数据库操作接口 **/
	private BaseDao<IfAlarmWorkOrder> dao;
	/** 接口告警工单号REDIS处理器 **/
	private WorkOrderNoJedisProxy workOrderNoJedisProxy;

	@Override
	public IfAlarmWorkOrder generateWorkOrder(IfAlarm alarm)
			throws JsonParseException, JsonMappingException, IOException {
		Date now = new Date();
		IfAlarmWorkOrder order = new IfAlarmWorkOrder();
		// 设置工单号
		order.setCode(genereateWoNo(alarm, now));
		order.setStatus(WoStatus.NOT_DEAL.ValueCode);
		order.setAssignDate(now);
		order.setDealPerson(alarm.getErrorReceiver());
		order.setEmailReceiver(alarm.getEmailReceiver());

		order.setIfId(alarm.getIfId());
		order.setIsEnable(EnableType.ENABLE.ValueCode);
		order.setCreator(alarm.getCreator());
		order.setCreateDate(alarm.getCreateDate());
		order.setModifier(alarm.getModifier());
		order.setLastUpDate(alarm.getLastUpdatedDate());
		return order;
	}

	@Override
	public String genereateWoNo(IfAlarm alarm, Date date)
			throws JsonParseException, JsonMappingException, IOException {
		String key = DateUtils.DATEPATTERN_YYYYMMDD.format(date);
		long no = workOrderNoJedisProxy.get(key);
		String type = alarm.getAlarmType();
		String level = alarm.getAlarmLevel();
		// 流水长度为5个字符
		String woNo = (no + "").format("%05d", no);
		// 告警类型长度为2个字符
		type = type.format("%02d", Integer.parseInt(type));
		// 告警等级长度为2个字符
		level = level.format("%02d", Integer.parseInt(level));
		return Constants.WO_SUFFIX + key + type + level + woNo;
	}

	@Override
	public void save(List<IfAlarmWorkOrder> orders)
			throws PersistentDataException {
		if (orders == null || orders.isEmpty()) {
			return;
		}
		dao.saveAll(orders);

	}

	public WorkOrderNoJedisProxy getWorkOrderNoJedisProxy() {
		return workOrderNoJedisProxy;
	}

	public void setWorkOrderNoJedisProxy(
			WorkOrderNoJedisProxy workOrderNoJedisProxy) {
		this.workOrderNoJedisProxy = workOrderNoJedisProxy;
	}

	public BaseDao<IfAlarmWorkOrder> getDao() {
		return dao;
	}

	public void setDao(BaseDao<IfAlarmWorkOrder> dao) {
		this.dao = dao;
	}

}
